import request from '@/utils/request'

export default {
  // 用户管理
  getAreaList(searchModel) {
    const encodedSearchKey = searchModel.search_key;
    const params = JSON.stringify({
      page: searchModel.page,
      pageSize: searchModel.pageSize,
      search_key: encodedSearchKey,
    });
    console.log(params);
    // 创建 URLSearchParams 对象并设置参数
    return request({
      url: "/planting-area/pageList",
      method: "post",
      headers: {
        // 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
        "Content-Type": "application/json",
      },
      data: params,
      // params
    });
  },
  // 用户管理 分页列表
  // export function AreaPageList(data) {
  //   return request({
  //     url: '/area/pageList',
  //     method: 'post',
  //     data
  //   })
  // },

  // 添加区域
  addArea(area) {
    return request({
      url: '/planting-area/add',
      method: 'post',
      data: area
    });
  },
  // 修改区域
  updateArea(area) {
    return request({
      url: '/planting-area/update',
      method: 'post',
      data: { ...area }
    });
  },
  updateUseTree(planting_area_id, planting_tree_ids) {
    return request({
      url: '/planting-area/updateUseTree',
      method: 'post',
      data: { planting_area_id, planting_tree_ids }
    });
  },

  saveArea(area) {
    // if(area.id == null && area.id == undefined){
    //   return this.addArea(area);
    // }
    // 添加
    if (!area.id) {
      return this.addArea(area);
    }
    return this.updateArea(area);
  },
  // 删除
  deleteAreaById(id) {
    return request({
      url: `/planting-area/delete`,
      method: 'post',
      data: { id }
    });
  },
}
